//
//  YHNormalTableViewCell.swift
//  Adorbee
//
//  Created by amovision on 2025/7/21.
//

import UIKit
#if canImport(SnapKit)
import SnapKit
#endif

open class YHNormalTableViewModel {
  public var leftImage: Any?
  public var title: Any
  public var subtitle: Any?
  public var detail: Any?
  public var rightImage: Any?

  public init(title: Any, leftImage: Any? = nil, subtitle: Any? = nil, detail: Any? = nil, rightImage: Any? = nil) {
    self.title = title
    self.leftImage = leftImage
    self.subtitle = subtitle
    self.detail = detail
    self.rightImage = rightImage
  }
  
  /// 更新模型属性
  open func update() {}
}

open class YHNormalTableViewCell<T: YHNormalTableViewModel>: YHBaseTableViewCell<T> {
  public let leftImage = UIImageView()
  public let titleLabel = UILabel(font: .num(14), color: .label, lines: 0)
  public let subtitleLabel = UILabel(font: .num(11), color: .label1, lines: 0)
  public let detailLabel = UILabel(font: .num(14), color: Theme.light, lines: 0)
  public let rightImage = UIImageView()

  open override func initContent() {
    contentView.addSubviews([leftImage, titleLabel, subtitleLabel, detailLabel, rightImage])
#if canImport(SnapKit)
    leftImage.snp.makeConstraints { make in
      make.left.equalTo(16)
      make.centerY.equalToSuperview()
    }
    titleLabel.snp.makeConstraints { make in
      make.left.equalTo(16)
      make.top.equalTo(16)
      make.right.lessThanOrEqualTo(rightImage.snp.left).offset(-8)
    }
    rightImage.snp.makeConstraints { make in
      make.right.equalTo(-16)
      make.centerY.equalTo(titleLabel)
    }
    detailLabel.snp.makeConstraints { make in
      make.right.equalTo(-16)
      make.centerY.equalTo(titleLabel)
    }
    subtitleLabel.snp.makeConstraints { make in
      make.left.equalTo(titleLabel)
      make.bottom.equalTo(-16)
      make.right.lessThanOrEqualTo(-16)
    }
#endif
  }

  open override func initModel(_ model: T) {
    model.update()
    titleLabel.string = model.title
    detailLabel.string = model.detail
    subtitleLabel.string = model.subtitle
    leftImage.image = UIImage.named(model.leftImage)
    rightImage.image = UIImage.named(model.rightImage)
#if canImport(SnapKit)
    titleLabel.snp.updateConstraints { make in
      make.left.equalTo(leftImage.image != nil ?(leftImage.image!.width + 24) : 16)
    }
    detailLabel.snp.updateConstraints { make in
      make.right.equalTo(rightImage.image != nil ? (-(rightImage.image!.width + 24)) : (-16))
    }
#endif
  }
  
  open override class func cellHeight(_ model: T) -> CGFloat {
    let titleheight = UIFont.num(14).lineHeight
    let subHeight: CGFloat = if let subtitle = model.subtitle as? String {
      AString(subtitle, font: .num(11), color: .gray(4)).height(YHScreenW-32-YHCellInset*2)
    } else if let subtitle = model.subtitle as? AString {
      subtitle.height(YHScreenW-32-YHCellInset*2)
    } else { 0 }
    let titleGap: CGFloat = subHeight == 0 ? 0 : 10
    return titleheight + titleGap + subHeight + 32
  }
  
}
